<?php

/**
 * Created by PhpStorm.
 * User: henry
 * Date: 2017/2/22
 * Time: 下午6:25
 */

namespace business\base\services;

use business\base\models\Region;
use business\base\models\RegionNode;
use yii\mongodb\Query;

class DictionaryService
{
    private static $database = "b2b_parameter";
    private static $collection_Name = "infr_dictionary";

    public static $TYPE_REGION = "region";

    /**
     * @param $code
     * @return RegionNode
     */
    public function getRegionHierarchyByCode($code)
    {
        $query = new Query();
        $data = $query->from([$this::$database, $this::$collection_Name])->where([
            "item_code" => $code,
            "item_type" => static::$TYPE_REGION
        ])->one();
        if (empty($data)) {
            return new RegionNode();
        }
        $region = new RegionNode();
        $region->code = $data["item_code"];
        $region->displayOrder = $data["display_order"];
        $region->hierarchyLevel = 0;//是否末级
        $region->isLeaf = false;// 级别
        $region->name = $data["item_name"];

        $region->parentCode = isset($data["parent_item_code"]) ? $data["parent_item_code"] : '';
        $region->area = isset($data["area"]) ? $data["area"] : '';

        $region->childRegionNodes = $this->getRegionListByProvince($region->code);

        return $region;
    }

    /**
     * @param string $id 省级Id
     * @return Region[]
     */
    public function getRegionListByProvince($id)
    {
        if (empty($id)) {
            return null;
        }
        $query = new Query();
        $data = $query->from([$this::$database, $this::$collection_Name])->where([
            'item_type' => 'region',
            'item_status' => 1,
            "parent_item_code" => $id
        ])
            ->orderBy(['display_order' => SORT_ASC])->all();
        $result = array();
        foreach ($data as $d) {
            $region = new Region();
            $region->code = $d["item_code"];
            $region->displayOrder = $d["display_order"];
            $region->name = $d["item_name"];
            $region->parentCode = isset($d['parent_item_code']) ? $d['parent_item_code'] : '';
            $region->area = isset($d["area"]) ? $d["area"] : '';
            array_push($result, $region);
        }
        return $result;
    }

    /**
     * @param string $code 省市Code
     * @return Region
     */
    public function getRegionByCode($code)
    {
        if (empty($code))
            return null;
        $query = new Query();
        $data=  $query->from([$this::$database, $this::$collection_Name])->where(['item_type' => 'region', 'item_status' => 1, "item_code"=> $code])->one();
        if(!empty($data)){
            $region=new Region();
            $region->code=$data["item_code"];
            $region->displayOrder=$data["display_order"];
            $region->name=$data["item_name"];
            if(array_key_exists("parent_item_code",$data))
            {
                $region->parentCode=$data["parent_item_code"];
            }else{
                $region->parentCode="";
            }
            return $region;
        }
        return null;
    }

    /**
     * 根据区域Code 获取对应区域名称
     * eg: code1,code2,code3
     * @param $codes
     * @param string $split
     * @return null|string
     */
    public function getAreaNameByCodes($codes,$split=',')
    {
        if (empty($codes)) {
            return null;
        }
        if (strtolower($codes) == "china") {
            return "全国";
        }
        $dictionaryService = new DictionaryService();
        $arrays = explode(',', $codes);
        $result = '';
        foreach ($arrays as $str) {
            $region = $dictionaryService->getRegionByCode($str);
            if (!empty($region)) {
                if (empty($result)) {
                    $result = $region->name;
                } else {
                    $result = $result .$split . $region->name;
                }
            }
        }
        return $result;
    }
}